<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/19
 * Time: 16:32
 */
class 实现解析网页并下载图片到本地
{

}

//error_reporting(0);//关闭错误报告
include_once("simple_html_dom.php");//引入解析库
$url='http://www.w3cschool.cc/mongodb/mongodb-window-install.html';
$html=file_get_html($url);
while($url[strlen($url)-1]!='/')$url=substr($url,0,-1);//获得上层目录
if(!is_dir($filepath)){mkdir($_SERVER['DOCUMENT_ROOT']."/imgtmp/",1);}//创建目录
foreach($html->find('img') as $im)
{
    $p=$im->src;
    if($p[0]=='/'||$p[0]=='\\')$p=$url."..".$p;//补全url
    file_put_contents("imgtmp/".md5(microtime()).".".substr($p,-3),file_get_contents($p));
}